package com.moxiao.yfjh.module.activity.controller.admin.merchanType;

import com.moxiao.yfjh.module.activity.common.GenActiveNoCommonUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;

import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;

import com.moxiao.yfjh.module.activity.controller.admin.merchanType.vo.*;
import com.moxiao.yfjh.module.activity.dal.dataobject.merchanType.MerchantTypeDO;
import com.moxiao.yfjh.module.activity.convert.merchanType.MerchantTypeConvert;
import com.moxiao.yfjh.module.activity.service.merchanType.MerchantTypeService;

@Tag(name = "管理后台 - 相亲商家类型")
@RestController
@RequestMapping("/activity/merchant-type")
@Validated
public class MerchantTypeController {

    @Resource
    private MerchantTypeService merchantTypeService;
    @Resource
    private GenActiveNoCommonUtil genActiveNoCommonUtil;

    @PostMapping("/create")
    @Operation(summary = "创建相亲商家类型")
    @PreAuthorize("@ss.hasPermission('activity:merchant-type:create')")
    public CommonResult<Long> createMerchantType(@Valid @RequestBody MerchantTypeCreateReqVO createReqVO) {
        genActiveNoCommonUtil.addMerchantTypeNo(createReqVO);
        return success(merchantTypeService.createMerchantType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新相亲商家类型")
    @PreAuthorize("@ss.hasPermission('activity:merchant-type:update')")
    public CommonResult<Boolean> updateMerchantType(@Valid @RequestBody MerchantTypeUpdateReqVO updateReqVO) {
        merchantTypeService.updateMerchantType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲商家类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('activity:merchant-type:delete')")
    public CommonResult<Boolean> deleteMerchantType(@RequestParam("id") Long id) {
        merchantTypeService.deleteMerchantType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲商家类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('activity:merchant-type:query')")
    public CommonResult<MerchantTypeRespVO> getMerchantType(@RequestParam("id") Long id) {
        MerchantTypeDO merchantType = merchantTypeService.getMerchantType(id);
        return success(MerchantTypeConvert.INSTANCE.convert(merchantType));
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲商家类型列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('activity:merchant-type:query')")
    public CommonResult<List<MerchantTypeRespVO>> getMerchantTypeList(@RequestParam("ids") Collection<Long> ids) {
        List<MerchantTypeDO> list = merchantTypeService.getMerchantTypeList(ids);
        return success(MerchantTypeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲商家类型分页")
    @PreAuthorize("@ss.hasPermission('activity:merchant-type:query')")
    public CommonResult<PageResult<MerchantTypeRespVO>> getMerchantTypePage(@Valid MerchantTypePageReqVO pageVO) {
        PageResult<MerchantTypeDO> pageResult = merchantTypeService.getMerchantTypePage(pageVO);
        return success(MerchantTypeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲商家类型 Excel")
    @PreAuthorize("@ss.hasPermission('activity:merchant-type:export')")
    @OperateLog(type = EXPORT)
    public void exportMerchantTypeExcel(@Valid MerchantTypeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MerchantTypeDO> list = merchantTypeService.getMerchantTypeList(exportReqVO);
        // 导出 Excel
        List<MerchantTypeExcelVO> datas = MerchantTypeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲商家类型.xls", "数据", MerchantTypeExcelVO.class, datas);
    }

}
